// pages/mine/room_register/bound_room/bound_room.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRoomList()
  },

  /**
   * 获取房间列表
   */
  getRoomList: function () {
    http.get('room/addresses/people_id',{
      area_code: app.globalData.areaCode,
      people_id: app.globalData.userInfo.people_id
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        let list = []
        res.data.data.forEach(item => {
          item.gmt_create = item.gmt_create.substr(0,10)
          list.push(item)
        })
        this.setData({
          roomList: list
        })
      }else {
        log.e("网络异常")
      }
    },err => {
      log.e(JSON.stringify(err))
      log.e("网络异常")
    })
  },

  /**
   * 设置默认房间
   * @param e
   */
  onRoomDefault: function (e) {
    const id = e.currentTarget.dataset.id
    Dialog.confirm({
      title: "提示",
      message: "确认将该房间设置为默认房间吗？"
    }).then(() => {
      //确认
      http.put("room/default",{
        room_id: id,
        people_id: app.globalData.userInfo.people_id,
        applet_id: app.globalData.appId
      }).then(res => {
        if (res.data.code === http.REQUEST_OK) {
          Toast.success("设置成功");
          this.getRoomList()
        }else {
          Dialog.alert({
            title: "设置失败",
            message: res.data.msg
          }).then(() => {

          })
        }
      }, err => {
        Toast.fail("网络异常")
      })
    }, () => {
      //取消
    })

  },

  /**
   * 继续添加
   */
  onAdd: function(e) {
    wx.navigateTo({
      url: '../steps_two/steps_two'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})